package com.diyphonegadgets.freeblog.data.parser;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;

import com.diyphonegadgets.freeblog.data.object.Article;


import android.content.res.XmlResourceParser;
import android.util.Log;
import android.util.Xml;


public class ParsingManager extends BasicParser implements Tags {
	private static final String TAG = "YAN_PARSING_MANAGER";
	
	
	// Feed Parsing Method
		public static List<Article> buildArticlesList(String response) {

			// Array of Episode Objects
			ArrayList<Article> episodes = null;
			
			

			try {
				
				String xml=response.replaceAll("<media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='", "<thumbnail>").replaceAll("' height='72' width='72'/>", "</thumbnail>");


				// Open a Connection to the feed
				XmlPullParser parser = Xml.newPullParser();
				try {
					parser.setInput(new StringReader(xml));
				} finally {

				}

				int event_type = parser.getEventType();
				Article current_episode = null;
				boolean done = false;

				// Parse the feed, start reading though the feed from top to
				// bottom
				while (event_type != XmlResourceParser.END_DOCUMENT && !done) {

					String tag_name = null;

					switch (event_type) {
					// Found the start of the feed
					case XmlResourceParser.START_DOCUMENT:
						episodes = new ArrayList<Article>();
						break;
					// Found a start tag
					case XmlResourceParser.START_TAG:

						// apply the data to our Episode object based on the tag
						// name
						tag_name = parser.getName();
						if (tag_name.equalsIgnoreCase("item")) {
							current_episode = new Article();
						} else if (current_episode != null) {
							if (tag_name.equals("title")) {
								current_episode.setTitle(parser.nextText());
							} else if (tag_name.equals("pubDate")) {
								current_episode.setPubDate(new Date(parser.nextText()));
							} else if (tag_name.equals("link")) {
								current_episode.setLink(parser.nextText());
							} else if (tag_name.equals("description")) {
								current_episode.setDescription(parser.nextText());

							} else if (tag_name.equals("thumbnail")) {
								
								current_episode.setThumbnailURL(parser.nextText());
							}
						}
						break;
					// An end tag has been reached
					case XmlResourceParser.END_TAG:
						tag_name = parser.getName();
						// End of an Episode Item
						if (tag_name.equalsIgnoreCase("item")
								&& current_episode != null) {
							episodes.add(current_episode);
							// Reached the end of all episodes, no more data to
							// collect
						} else if (tag_name.equalsIgnoreCase("channel")) {
							done = true;
						}
						break;
					}
					event_type = parser.next();
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}

			// Return the Episode Array
			return episodes;
		}




}